package org.beetl.json.node;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.beetl.json.AbstractOutputNode;
import org.beetl.json.Location;
import org.beetl.json.OutputNode;
import org.beetl.json.util.JsonUtil;

public class ClassNode extends AbstractOutputNode {

	Class c ;
	Location loc ;
	public  ClassNode(Class c,Location loc){
		this.c = c;
		this.loc = loc;
	}
	List<AbstractOutputNode > attrs = new ArrayList<AbstractOutputNode >();
	@Override
	public  void render(Object obj,Writer w,String after) throws IOException
	{
		w.write("{");
		for(AbstractOutputNode node:attrs){
			node.render(obj, w,",");			
		}
		w.write("}");
		
	}
	
	public void addAttributeNode(AbstractOutputNode node){
		this.attrs.add(node);
	}
	public List<AbstractOutputNode> getAttrs() {
		return attrs;
	}
	public void setAttrs(List<AbstractOutputNode> attrs) {
		this.attrs = attrs;
	}
	
	public void parse(){
		Method[] ms = c.getMethods();
		for(Method m:ms){
			if(JsonUtil.isGetterMethod(m)){	
				//@todo: list map,class,etc node
				if(m.getDeclaringClass()!=Object.class){
					Class type = m.getReturnType();
					AbstractOutputNode node  = null;
					if(List.class.isAssignableFrom(type)){
						node = new ListAttributeNode(m);
					}else{
						node = new AttributeNode(this.c,m);
					}
										
					node.scope(loc);
					this.addAttributeNode(node);
				}
				
				
			}
		}
	}

	@Override
	protected boolean isMatch(Location location) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	

}
